package com.donglan.pethome.pet.service.impl;

import com.donglan.pethome.basic.service.impl.BaseServiceImpl;
import com.donglan.pethome.basic.util.DistanceUtil;
import com.donglan.pethome.basic.util.Point;
import com.donglan.pethome.organization.domain.Shop;
import com.donglan.pethome.organization.mapper.ShopMapper;
import com.donglan.pethome.pet.domain.SearchMasterMessage;
import com.donglan.pethome.pet.mapper.SearchMasterMsgMapper;
import com.donglan.pethome.pet.service.SearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhangdonghui
 * @Date: 2022/2/14 16:41
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMessage> implements SearchMasterMsgService {


    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    /*
     * 功能描述 发布寻主信息
     * @author zhangdonghui
     * @date 2022/2/14 16:44
     * @param SearchMasterMessage
     * @return void
     */
    @Override
    public void publish(SearchMasterMessage searchMasterMessage) {

        // 获取发布地址
        String address = searchMasterMessage.getAddress();
        // 把地址转为经纬度
        Point point = DistanceUtil.getPoint(address);
        // 查询所有的店铺
        List<Shop> shops = shopMapper.loadAll();
        // 算处离我最近的店铺
        Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
        // 设置最近的店铺
        searchMasterMessage.setShop(nearestShop);
        // 保存寻主信息对象
        searchMasterMsgMapper.add(searchMasterMessage);
    }
}
